import { createStore } from 'vuex'
import persist from 'vuex-persistedstate'
export default createStore({
  state: {
    cartList: [], // 购物车的数据
  },
  mutations: { // 固话插件，监听的是 mutations 方法，只要mutations 方法被触发了，都会重新存储一下
    addCart(state, val) { // 向购物车添加数据
      let i = state.cartList.findIndex(item => item.id == val.id);
      // console.log(i);
      if (i>-1){ // 商品已经存在
        state.cartList[i].num++;
      } else{
        state.cartList.push(val);
      }
      
    },
    storeDel(state){
      // state.cartList = state.cartList.filter(item => item.ischeck == false);
      state.cartList = state.cartList.filter(item => !item.ischeck);
    },
    kong(state){
      // state.cartList = state.cartList.filter(item => item.price>=1000 && item<=2000)
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
    new persist({
      storage: window.localStorage,
    }),
  ],
})
